package cn.cstech.aijia;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.tendcloud.tenddata.TCAgent;

import cn.cstech.aijia.ui.base.utils.HarwkinLogUtil;
import cn.cstech.aijia.ui.base.utils.LocalUtils;
import cn.cstech.aijia.ui.base.utils.SPUtils;

/**
 * Created with Android Studio.
 * Title:TianYaGongZaiApplication
 * Description:
 * Copyright:Copyright (c) 2014
 * Company:中国平安健康保险有限公司
 * Author:Alice
 * Date:15/8/26
 * Time:上午11:53
 * Version 1.0
 */
public class AiJiaApplication extends BaseApplication {
    public LocationClient mLocationClient;
    public MyLocationListener mMyLocationListener;
    @Override
    public void onCreate() {
//        initBaiduMap();
//        initCurrentLocation();
        initTC();
        super.onCreate();
    }

    //初始化当前的城市
    private void initCurrentLocation(){
        mLocationClient = new LocationClient(this);
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll");
        option.setIsNeedAddress(true);
        option.setOpenGps(true);
        option.setScanSpan(1000);
        mLocationClient.setLocOption(option);
        mMyLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mMyLocationListener);
        mLocationClient.start();
    }

    //初始化百度地图
    private void initBaiduMap(){
        SDKInitializer.initialize(this);
    }

    //初始化talkingdata
    private void initTC(){
        TCAgent.LOG_ON = true;
        TCAgent.init(this,ThirdPartyConstants.TD_APP_ID, LocalUtils.getChannel(this));
        TCAgent.setReportUncaughtExceptions(true);
    }
    //当前位置监听器
    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if(location == null || location.getLongitude() == 0 || location.getLatitude() == 0){
                HarwkinLogUtil.info("Harwkin","Invalid Location");
                return ;
            }
            float lat  = (float)location.getLatitude();
            float lng = (float)location.getLongitude();
            String city = location.getCity();
            String address = location.getAddrStr();
            HarwkinLogUtil.info("Harwkin", "lat =" + lat + ",lng =" + lng + ",city =" + city + ",address = " + address);
            if (location.getLocType() == BDLocation.TypeGpsLocation){
                //GPS 返回的地址信息
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
                //网络提供者返回的地址
            }
            SPUtils.setLocation(AiJiaApplication.this,address,city,lat,lng);
        }
    }
}
